package com.eCommerce.server.dao.impl;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;

import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.eCommerce.corecontract.criteria.CategorieCriteria;
import com.eCommerce.corecontract.dto.CategorieDTO;

import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommercePersisenceException;
import com.eCommerce.server.corebackend.dao.AbstractCommonHibernateDAO;
import com.eCommerce.server.corebackend.dao.ICategorieDAO;





public class CategorieDAOImpl extends AbstractCommonHibernateDAO implements ICategorieDAO{
	private static Logger log =Logger.getLogger(CategorieDAOImpl.class.getName());
	
	public void delete(CategorieDTO categorieDTO) throws EcommerceException {
	
		
		try {
				Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			   String hql = "delete from CategorieDTO where id = :id";
		        Query query = session.createQuery(hql);
		        query.setLong("id",categorieDTO.getId());
		        int rowCount = query.executeUpdate();
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCATEGORIE-005",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}

	
	public List find(CategorieCriteria categorieCriteria) throws EcommerceException {
		// TODO Auto-generated method stub
		
		try {
			DetachedCriteria criteria= DetachedCriteria.forClass(CategorieDTO.class);	
			criteria.add(Restrictions.eq("libelle", categorieCriteria.getLibelle()));
			Set<CategorieDTO> listOfCategorieDTO= new HashSet<CategorieDTO>();	
			List listOf=getHibernateTemplate().findByCriteria(criteria);		
			
			
			return listOf;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			Object[] arg=new Object[1];
			arg[0] = categorieCriteria.getLibelle();
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCATEGORIE-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
	}

	
	public List findAll() throws EcommerceException {
		// TODO Auto-generated method stub
		
		try {
		
			DetachedCriteria criteria= DetachedCriteria.forClass(CategorieDTO.class);
			List listOfCategorie=getHibernateTemplate().findByCriteria(criteria);
		
			
			return listOfCategorie;
		
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCATEGORIE-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
		
	}

	
	public CategorieDTO findOne(CategorieDTO categorieDTO)	throws EcommerceException {

		
		try {
			categorieDTO = (CategorieDTO)getHibernateTemplate().get(CategorieDTO.class, categorieDTO.getId());
			return categorieDTO;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			Object[] arg=new Object[1];
			arg[0] = categorieDTO.getLibelle();
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCATEGORIE-003",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}


	public Long insert(CategorieDTO categorieDTO)
			throws EcommerceException {
		
		
		try {
			getHibernateTemplate().save(categorieDTO);
			return categorieDTO.getId();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			arg[0] = categorieDTO.getLibelle();
			EcommercePersisenceException persistenceException = new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCATEGORIE-001",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
	}


	public void update(CategorieDTO categorieDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		
		try {
			getHibernateTemplate().saveOrUpdate(categorieDTO);
			getHibernateTemplate().getSessionFactory().getCurrentSession().flush(); 
			getHibernateTemplate().getSessionFactory().getCurrentSession().clear();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			arg[0] = categorieDTO.getLibelle();
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCATEGORIE-002",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}

}
